Skip to content

Car Fleet ​

Car Fleet — LeetCode

Cars travel towards the same destination at different speeds and cannot overtake; count how many distinct fleets arrive.

Approach ​

Sort the values by position descendng. Calculate the eta they arrive at. If stack is empty or top of stack has a value less than eta, push to stack. return count of stack.

Better way: Sort, while iterating, check if prevETA is bigger than current, then set fleet++. Return fleet.

Remarks ​